res_mod2 <- lapply(res_mod2, function(res_tab){
res_tab$gene <- rownames(res_tab)
res_tab
})
res_mod1 <- lapply(res_mod1, function(res_tab){
res_tab$gene <- rownames(res_tab)
res_tab
})
case_sig <- function(FDR1, FDR2, com1, com2, FDR_t){
case_when(
FDR1 <= FDR_t & !! FDR2 <= FDR_t ~ "both",
FDR1 <= FDR_t & !! FDR2 > FDR_t ~ com1,
FDR1 > FDR_t & !! FDR2 <= FDR_t ~ com2,
FDR1 > FDR_t & !! FDR2 > FDR_t ~ "none"
)
}
## plotting function
logFC_plot <- function(log_FC_tab,
comp1,
comp2,
FDR_t = 0.1,
logFC_both = 0.5){
# subset dataframe
log_FC_sub <- log_FC_tab |>
select(c(logFC,FDR,gene, tech)) |>
pivot_wider(names_from = tech,
values_from = c(logFC, FDR))
# new colum names
logFC_comp1 <- grep(paste0("logFC_", comp1), colnames(log_FC_sub), value = T)
logFC_comp2 <- grep(paste0("logFC_", comp2), colnames(log_FC_sub), value = T)
FDR_comp1 <- grep(paste0("FDR_", comp1), colnames(log_FC_sub), value = T)
FDR_comp2 <- grep(paste0("FDR_", comp2), colnames(log_FC_sub), value = T)
# Add sig threshold
log_FC_sub <- log_FC_sub |>
mutate(sig = case_sig(FDR1 = !!sym(FDR_comp1),
FDR2 = !!sym(FDR_comp2),
com1 = comp1,
com2 = comp2,
FDR_t = FDR_t))
# Add genes to highlight
log_FC_sub <- log_FC_sub |> mutate(
highlight = case_when(
sig %in% c("both") &
sign(!!sym(logFC_comp1)) == sign(!!sym(logFC_comp2)) ~ gene,
.default = ""
)
)
# colors
color_pal <- c("grey","#fdb462", "#ff6666","#0099cc")
names(color_pal) <- c("none", "both", comp1, comp2)
# plot
p <- ggplot(log_FC_sub, aes_string(x=logFC_comp1,
y=logFC_comp2)) +
geom_point(aes(colour = sig), size = 0.9, alpha = 0.7) +
geom_vline(xintercept = 0) +
geom_hline(yintercept = 0) +
#xlim(0, 1) +
#ylim(0, 1) +
scale_color_manual(values = color_pal) +
theme_bw()
# annotations
#p + geom_text_repel(
# aes(label = highlight),
# family = "Poppins",
# size = 2,
# min.segment.length = 0,
# seed = 42,
# box.padding = 0.5,
# max.overlaps = Inf,
# arrow = arrow(length = unit(0.005, "npc")),
# nudge_x = .15,
# nudge_y = .5,
# color = "#3D3D3D"
#)
p
}